using System;
using Rapier.Kodachi.Shared;
using System.Net;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Web;

namespace Rapier.Kodachi.Weather.Actions
{
    public class CurrentWeather : Action
    {
        #region Constructor
        public CurrentWeather(Plugin plugin)
            : base(plugin, "Rapier.Kodachi.Weather.CurrentWeather", "Current Weather", "Get the current forecast for your location.")
        {
            this.DefaultIndirectTextMode = true;
            this.IndirectItemUse = IndirectItemUse.Optional;
            GetImage = delegate() { return (Bitmap)Rapier.Kodachi.Weather.Properties.Resources._30; };
        }
        #endregion

        #region Action Members
        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            string address = string.Empty;
            if (indirectItems.Length > 0)
            {
                address = @"http://xoap.weather.com/weather/local/" + indirectItems[0].Properties["Text"].ToString() + "?cc=*&dayf=10&prod=xoap&par=1007433109&key=f15e225f602adebb";
                if (WeatherPlugin.WeatherSettings.UseCelsius)
                    address += "&unit=m";
            }
            else
            {
                address = @"http://xoap.weather.com/weather/local/" + WeatherPlugin.WeatherSettings.DefaultLocationID + "?cc=*&dayf=10&prod=xoap&par=1007433109&key=f15e225f602adebb";
                if (WeatherPlugin.WeatherSettings.UseCelsius) 
                    address += "&unit=m";
            }

            
            DataSet weatherDS = WeatherShared.GetWeatherData(address);

            
            //PrintDataSet(weatherDS);  //for testing the dataset only...
            SendWeatherNotification(weatherDS);
        }
        public override bool IsValidIndirect(Item indirectItem)
        {
            if (indirectItem.Properties.ContainsKey("Text"))
                return Microsoft.VisualBasic.Information.IsNumeric(indirectItem.Properties["Text"].ToString()) && indirectItem.Properties["Text"].ToString().Length == 5;
            else
                return false;
        }

        public override bool IsValidDirect(Item directItem)
        {
            return directItem.Properties.ContainsKey("Weather") && (bool)(directItem.Properties["Weather"]) == true;
        }
        #endregion

        #region Private Methods
        private void SendWeatherNotification(DataSet weatherDS)
        {
            NotificationPackage package = new NotificationPackage();
            package.CaptionClick += new PackageCaptionClickHandler(package_Click);
            package.MessageClick += new PackageMessageClickHandler(package_Click);
            try
            {
                if (weatherDS != null && !weatherDS.Tables.Contains("err"))
                {
                    package.Message = "Current Temperature: " + weatherDS.Tables["cc"].Rows[0]["tmp"].ToString() +
                        "\nFeels Like: " + weatherDS.Tables["cc"].Rows[0]["flik"].ToString();
                    package.Caption = weatherDS.Tables["loc"].Rows[0]["dnam"].ToString();
                    package.Image = new System.Drawing.Bitmap((Bitmap)Rapier.Kodachi.Weather.Properties.Resources.ResourceManager.GetObject("_" + weatherDS.Tables["cc"].Rows[0]["icon"].ToString()), new Size(48, 48));
                    Plugin.Host.Notify(package);

                }
                else
                {
                    package.Message = "Current Weather Not Available\nPlease verify that you have set a valid default weather location in the plugin settings.";
                    package.Caption = "Current Weather Not Available";
                    Plugin.Host.Notify(package);
                }
            }
            catch
            {
                package.Message = "Could not retrieve current weather.\nPlease verify that you have set a valid default weather location in the plugin settings.";
                package.Caption = "Current Weather Not Available";
                Plugin.Host.Notify(package);
            }
        }

        private void package_Click(NotificationPackage sender)
        {
            string url = String.Format("http://weather.com/weather/local/{0}", HttpUtility.UrlEncode(WeatherPlugin.WeatherSettings.DefaultLocationID));
            Process.Start(url);
        }
        #endregion
    }
}
